﻿using System;
using System.Collections.Generic;
using O2DESNet;
using ShkSimulation.core.dao.statistic;
using ShkSimulation.core.template.model;
using ShkSimulation.carsharing.impl.model;

namespace ShkSimulation.carsharing.impl.elements {
    
    public partial class Road {
        
        public Road(BaseAgencyParams baseAgencyParams) : base(baseAgencyParams) {
            ServingVehicleCounter = AddHourCounter();
            QueuingVehicleCounter = AddHourCounter();
            RoadVehicleCounter = AddHourCounter();
            ServedCustomerNumber = AddHourCounter();
            VehicleInVirtualSpace = AddHourCounter();
            ServiceTimeCounter = AddHourCounter();
            ServiceIntervalCounter = AddHourCounter();
            RoadDispatcherCounter = AddHourCounter();
            
            reportRelatedProperty["车辆数"] = RoadVehicleCounter.AsReadOnly();
            //reportRelatedProperty["服务完的顾客数"] = ServedCustomerNumber.AsReadOnly();
            //reportRelatedProperty["排队长度"] = QueuingVehicleCounter.AsReadOnly();
            //reportRelatedProperty["服务时间"] = ServiceTimeCounter.AsReadOnly();
            //reportRelatedProperty["服务间隔"] = ServiceIntervalCounter.AsReadOnly();
            ServiceRateHelper = new RoadServiceRateHelper(this);
        }
        
        public RoadServiceRateModeEnum ServiceRateMode { get; set; }
        
        public double Utilization { get; set; }
        public double Capacity { get; set; }
        public double FreeSpeed { get; set; }
        public double RoadLength { get; set; }
        public int VehicleCount => (int)RoadVehicleCounter.LastCount;
        public int DispatcherCount => (int)RoadDispatcherCounter.LastCount;
        public double Price { get; set; }
        public double MaxPrice { get; set; }
        public double FuelCost { get; set; }
        public double ServiceDemandFactor { get; set; }
        public double PotentialDemand { get; set; }
        HourCounter ServedCustomerNumber { get; set; }
        HourCounter ServingVehicleCounter { get; set; }
        HourCounter QueuingVehicleCounter { get; set; }
        HourCounter RoadVehicleCounter { get; set; } 
        HourCounter VehicleInVirtualSpace { get; set; }
        HourCounter ServiceTimeCounter { get; set; }
        HourCounter ServiceIntervalCounter { get; set; }
        HourCounter RoadDispatcherCounter { get; set; }
        
        public override ServiceRateHelper ServiceRateHelper { get; set; }

        public static readonly int SERVICE_RATE_UPPER = 50;
        public static readonly int SERVICE_RATE_LOWER = 1;
        
        private Dictionary<string, ReadOnlyHourCounter> reportRelatedProperty = new Dictionary<string, ReadOnlyHourCounter>();
        private Dictionary<string, double> reportRelatedDoubleData =
            new Dictionary<string, double>();
        
        /**
        * @description: DataReport
        */
        public override DataReportInfo GetReportInfo() {
            //reportRelatedDoubleData["调度员到达数"] = RoadDispatcherCounter.TotalIncrement;
            return DataReportInfo.Create(Uniquekey, ClockTime, reportRelatedProperty, reportRelatedDoubleData);
        }

        public override string ToString() { 
            return $"{"唯一Key"}: {UniqueId}\n" +
                   $"{"车辆数"}: {VehicleCount}\n" +
                   $"{"服务完的顾客数"}: {ServedCustomerNumber.LastCount}\n" +
                   $"{"虚拟空间车辆数"}: {VehicleInVirtualSpace.LastCount}\n" +
                   $"{"等待服务顾客数"}: {WaitServeList.Count}\n" +
                   $"{"平均服务时间"}: {TimeSpan.FromHours(ServiceTimeCounter.AverageCount)}\n" +
                   $"{"平均服务间隔"}: {TimeSpan.FromHours(ServiceIntervalCounter.AverageCount)}\n" +
                   "\n" +
                   $"{"上游站点"}: {LastStationId}\n" +
                   $"{"下游站点"}: {NextStationId}\n" +
                   $"{"道路利用率"}: {Utilization}\n" +
                   $"{"道路容量"}: {Capacity}\n" +
                   $"{"自由速度"}: {FreeSpeed}\n" +
                   $"{"道路长度"}: {RoadLength}\n" +
                   $"{"价格"}: {Price}\n" +
                   $"{"最大价格"}: " + $"{MaxPrice}\n" +
                   $"{"每公里耗油"}: {FuelCost}\n" +
                   $"{"潜在需求"}: {PotentialDemand}\n";
        }
    }
}
